//
//  OPTopic.h
//  OPFramework
//
//  Created by malcom on 7/12/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "OPBaseObject.h"
#import "OPPost.h"
#import "OPUser.h"
#import "OPPageContainer.h"

@class OPForum;
@class OPPost;
@interface OPTopic : OPBaseObject {
	OPForum					*obj_parentForum;	// parent forum
	OPUser					*p_postAuthor;		// the author of the topic
	OPUser					*p_lastPoster;		// the author of the last message

	OPPageContainer			*obj_postsPages;	// cached pages of the topic
	NSNumber				*p_topicID;			// topic unique id
	
	NSString				*p_topicTitle;		// topic subject
	NSDate					*p_creationDate;	// topic creation date
	NSNumber				*p_topicViews;		// number of views of this topic
	NSNumber				*p_topicReplies;	// number of replies (+1 = number of posts)
	NSNumber				*p_topicStatus;		// topic status
	NSDate					*p_lastViewDate;	// last view date
	NSDate					*p_lastPostDate;	// last post sending date

	NSString				*p_topicPassword;	// password key (if it's protected)
	NSNumber				*_firstPostID;
	NSNumber				*_lastPostID;
	NSNumber				*p_bookmarked;
	
	NSNumber				*p_firstNewPageSinceLastVisit;
	NSNumber				*p_firstNewPostIDSinceLastVisit;
}

@property (readonly) OPPageContainer			*obj_postsPages;
@property (assign)	NSNumber	*p_topicID;
@property (assign)	OPForum		*obj_parentForum;
@property (assign)	NSString	*p_topicPassword;
@property (assign)	NSString	*p_topicTitle;
@property (assign)	NSDate		*p_creationDate;
@property (assign)	NSNumber	*p_topicViews;
@property (assign)	NSNumber	*p_topicReplies;
@property (assign)	NSNumber	*p_topicStatus;
@property (assign)	NSDate		*p_lastViewDate;
@property (assign)	NSDate		*p_lastPostDate;
@property (assign)	NSNumber	*_firstPostID;
@property (assign)	NSNumber	*_lastPostID;
@property (assign)	OPUser		*p_postAuthor;
@property (assign)	OPUser		*p_lastPoster;
@property (readonly) NSNumber	*p_bookmarked;
@property (assign) NSNumber		*p_firstNewPageSinceLastVisit;
@property (assign) NSNumber		*p_firstNewPostIDSinceLastVisit;

#pragma mark INIT
- (id) initWithTopicID:(NSNumber *) _topicID fromForum:(OPForum *) _forum; // init without loading (use for async with refreshData)
- (id) initLoadingTopicWithID:(NSNumber *) _topicid fromForum:(OPForum *) _forum password:(NSString *) _password forceLoad:(BOOL) _force;
- (id) initLoadingPageWithPost:(NSNumber *) _postid ofTopic:(NSNumber *) _topicid fromForum:(OPForum *) _forum password:(NSString *) _password;
- (id) initLoadingPage:(NSNumber *) _pageIdx ofTopic:(NSNumber *) _topicid fromForum:(OPForum *) _forum password:(NSString *) _password forceLoad:(BOOL) _force;

#pragma mark GET


- (OPPost *) getLastPost;

/*!
    @method     getPostWithID:onlyInCache:
    @abstract   This method will return an OPPost with requested uid
    @discussion If you specify _cached as YES it will search only inside internal cache. If an item was not found a nil result will be released.
				Otherwise this method will perform a sync XML request to get your requested post and return an OPPost (or nil if your uid does not exist).
	@param		_uid the uid of the message to show
	@param		_cached search only in cache or perform a sync XML request
	@result		the OPPost object requested
*/

- (OPPost *) getPostWithID:(NSNumber *) _uid forceLoad:(BOOL) _force;

/*!
    @method     getPostsInPage:usingCache:
    @abstract   This method call an xml request to get a list of OPPost visible in a specified page
    @discussion If you pass _cache as NO this method perform a new request even if the data is available in
				OFAPI internal cache. If you are working in sync mode this method will block your main thread
				but the results is the requested array. If you are working on async mode this method return a nil
				value and your listeners will be alerted using OPObject_AsyncResponse protocol.
	@result		an NSArray in sync mode or nil if you ware working in async mode
	@param		_pgIdx the index of page to show
	@param		_cache want you to use the data in cache if available (not if the cache is expired new data will be requested)
*/

- (NSArray *) getPostsInPage:(int) _pgIdx;

- (NSArray *) getLastPostsPage;
- (BOOL) toggleSetAsBookmark;

#pragma mark PRIVATE METHODS
- (void) _loadWithData:(NSDictionary *) _data;
- (NSArray *) _loadPostsInPage:(NSNumber *) _pageIdx forceSyncMode:(BOOL) _sync;
- (NSArray *) _loadPostsInPageFromResponse:(NSArray *) _list;
- (void) _parseLoadPostsAtPageRequest:(NSArray *) topicsInPage;
- (id) _initWithDataResponse:(NSDictionary *) _data ofForum:(OPForum *) _forum;
- (id) _initLinkedToForum:(OPForum *) _forum;
- (void) _parseSetBookmark:(NSArray *) _result;
- (void) _parseTopicDataRefresh:(NSArray *) _result;
- (NSArray *) _getParamsListForTopicInfo;

@end
